using System;
using ebcpatterns.flow;
using NUnit.Framework;

namespace ebcpatterns.tests.flow
{
    [TestFixture]
    public class SplitTests
    {
        [Test]
        public void Split_splits_the_input_into_two_outputs() {
            var sut = new Split<int, int, int>(x => x / 3, y => y / 4);

            var result1 = 0;
            var result2 = 0;
            sut.Output1 += x => result1 = x;
            sut.Output2 += x => result2 = x;
            sut.Input(24);

            Assert.That(result1, Is.EqualTo(8));
            Assert.That(result2, Is.EqualTo(6));
        }

        [Test]
        public void Split_finds_the_getter_of_the_input_type() {
            var sut = new Split<Tuple<int, int>, int, int>();

            var result1 = 0;
            var result2 = 0;
            sut.Output1 += x => result1 = x;
            sut.Output2 += x => result2 = x;
            sut.Input(new Tuple<int, int>(1, 2));

            Assert.That(result1, Is.EqualTo(1));
            Assert.That(result2, Is.EqualTo(2));
        }

        [Test]
        public void Split_without_mapping_input_to_output_type() {
            var sut = new Split<string, string, string>();

            var result1 = "";
            var result2 = "";
            sut.Output1 += x => result1 = x;
            sut.Output2 += x => result2 = x;
            sut.Input("a");

            Assert.That(result1, Is.EqualTo("a"));
            Assert.That(result2, Is.EqualTo("a"));
        }

        [Test]
        public void Split_without_message_data() {
            var sut = new Split();

            var count1 = 0;
            var count2 = 0;
            sut.Output1 += () => count1++;
            sut.Output2 += () => count2++;

            sut.Input();

            Assert.That(count1, Is.EqualTo(1));
            Assert.That(count2, Is.EqualTo(1));
        }

        [Test]
        public void Split_finds_the_getter_of_the_input_types() {
            var sut = new Split<Tuple<int, string>, int, Tuple<int, string>>();

            var result1 = 0;
            var result2 = new Tuple<int, string>(0, "");
            sut.Output1 += x => result1 = x;
            sut.Output2 += x => result2 = x;
            sut.Input(new Tuple<int, string>(1, "2"));

            Assert.That(result1, Is.EqualTo(1));
            Assert.That(result2, Is.EqualTo(new Tuple<int, string>(1, "2")));
        }
    }
}